import { ConfigEnv, defineConfig, loadEnv, UserConfig } from "vite";
import react from "@vitejs/plugin-react";
import * as path from "path";
import { warppEnv } from "./src/utils/getEnv";

// https://vite.dev/config/
export default defineConfig((mdoe: ConfigEnv): UserConfig => {
  const env = loadEnv(mdoe.mode, process.cwd());
  const viteEnv = warppEnv(env);
  return {
    plugins: [react()],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      host: "0.0.0.0",
      port: viteEnv.VITE_PORT, // 获取.env配置中的参数
      open: viteEnv.VITE_OPEN, // 获取.env配置中的参数
      proxy: {
        // "/api": {
        //   target: viteEnv.VITE_PROXY, // 获取.env配置中的参数
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/api/, ""),
        // }
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
    },
  };
});
